package com.android.dfsc;

import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

public class SettingProcess extends CommonProcess {

	public SettingProcess(Activity context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void setLayout() {
		// TODO Auto-generated method stub
		SharedPreferences   clientConfig = context.getSharedPreferences("client_config",0);
		
		setViews();
		RelativeLayout mRelativeLayout = (RelativeLayout) context
				.findViewById(R.id.relativeLayout2);
		
		LinearLayout mainLayout= (LinearLayout) context
				.findViewById(R.id.mainLinearLayout);
		mRelativeLayout.setVisibility(View.GONE);
		//mainLayout.removeView(mRelativeLayout);
		
		mRelativeLayout = (RelativeLayout) context
				.findViewById(R.id.relativeLayout3);
		mRelativeLayout.setVisibility(View.GONE);
		//mainLayout.removeView(mRelativeLayout);
		
		LayoutInflater inflater = (LayoutInflater) context
				.getApplicationContext().getSystemService(
						Context.LAYOUT_INFLATER_SERVICE);
		//final RelativeLayout myLayout = (RelativeLayout) inflater.inflate(
		//		R.layout.setting, null);
		final View settingView = inflater.inflate(R.layout.setting, null);
		mainLayout.addView(settingView);
		
		ImageView imageView=(ImageView)settingView.findViewById(R.id.imageView3);
		imageView.setAlpha(100);
		 
		EditText ipSetting = (EditText) settingView
				.findViewById(R.id.IPSetting);
                ipSetting.setText( clientConfig.getString("IP",""));
                
                ((EditText) settingView
				.findViewById(R.id.UserNameSetting)).setText(clientConfig.getString("user_name",""));
                
                ((EditText) settingView
        				.findViewById(R.id.PasswordSetting)).setText(clientConfig.getString("password",""));
                
                ((EditText) settingView
        				.findViewById(R.id.AuthCodeSetting)).setText(clientConfig.getString("auth_code",""));
                
                ((EditText) settingView
        				.findViewById(R.id.MasterPwdSetting)).setText(clientConfig.getString("master_password",""));
                
                ((EditText) settingView
        				.findViewById(R.id.SlavePwdSetting)).setText(clientConfig.getString("slave_password",""));
                
                
                ((TextView) settingView
				.findViewById(R.id.AuthString)).setText(SmartHomeEngine.getInstance().getAuthCode());
		
		
		Button bt = (Button)settingView.findViewById(R.id.button_save);
		bt.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				SharedPreferences   clientConfig =  context.getSharedPreferences("client_config",0);
        		Editor clientConfigEditor = clientConfig.edit();
        		
        		
        		clientConfigEditor.putString("IP",((EditText) settingView
        				.findViewById(R.id.IPSetting)).getText().toString() );
        		
        		clientConfigEditor.putString("user_name",((EditText) settingView
        				.findViewById(R.id.UserNameSetting)).getText().toString() );
        		clientConfigEditor.putString("password",((EditText) settingView
        				.findViewById(R.id.PasswordSetting)).getText().toString() );
        		clientConfigEditor.putString("auth_code",((EditText) settingView
        				.findViewById(R.id.AuthCodeSetting)).getText().toString() );
        		
        		clientConfigEditor.putString("master_password",((EditText) settingView
        				.findViewById(R.id.MasterPwdSetting)).getText().toString() );
        		
        		clientConfigEditor.putString("slave_password",((EditText) settingView
        				.findViewById(R.id.SlavePwdSetting)).getText().toString() );
        		
        		//clientConfigEditor.putString("auth_code","89ab4c5599635b0a" );
        		
        		clientConfigEditor.commit();
        		SmartHomeEngine.getInstance().setConfig();
			}
		});
		
	}

	@Override
	protected List<Map<String, Object>> getData() {
		// TODO Auto-generated method stub
		return null;
	}

}
